package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.TanhuaService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private TanhuaService tanhuaService;


    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 更换手机号----发送验证码
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        //获取当前登录用户手机号
        String phone = UserHolder.getMobile();
        //转发业务层,发送验证码
        userService.sendMsg(phone);
        return ResponseEntity.ok(null);

    }

    /**
     * 更换手机号---校验验证码
     *
     * @param map -- verificationCode
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        //获取验证码
        String code = (String) map.get("verificationCode");
        //获取当前登录用户手机号
        String phone = UserHolder.getMobile();
        //转发业务层,校验验证码
        Map rmap = userService.checkVerificationCode(phone, code);
        return ResponseEntity.ok(rmap);
    }


    /**
     * 更换手机号----保存
     *
     * @param map -- phone
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map) {
        //获取用户输入的新手机号
        String phone = (String) map.get("phone");
        //转发业务层,更新手机号
        userService.updatePhone(phone);
        return ResponseEntity.ok(null);
    }


    /**
     * 更新头像
     *
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity updateHead(MultipartFile headPhoto) throws IOException {
        //获取当前登录用户id
        Long userId = UserHolder.getUserId();
        //转发业务层更新头像
        userInfoService.updateHead(headPhoto, userId);
        return ResponseEntity.ok(null);
    }




    @GetMapping("/counts")
    public ResponseEntity counts() {

        Long userId = UserHolder.getUserId();

        Map map = userInfoService.counts(userId);

        return ResponseEntity.ok(map);

    }

    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable("type") Integer type,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                  String nickname){
        log.info("参数 nickname :{}",nickname);
       PageResult pr = userInfoService.findFriends(type,page,pagesize,nickname);
        return ResponseEntity.ok(pr);
    }

    @PostMapping("/fans/{uid}")
    public ResponseEntity fansLike(@PathVariable("uid")Long uid){
        //修改当前粉丝id对应的是否喜欢ta为true
        userInfoService.likeById(uid);
        return ResponseEntity.ok(null);
    }

    @DeleteMapping("/like/{uid}")
    public ResponseEntity loverDislike(@PathVariable("uid")Long uid){
        //修改当前喜欢的人id对应的是否喜欢ta为false
        userInfoService.dislikeById(uid);
        return ResponseEntity.ok(null);
    }
}
